const path = require('path');
const process = require('process');
const fs = require('fs');
// css样式从js文件中分离出来,需要通过命令行安装 extract-text-webpack-plugin依赖包
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin'); //引入清除文件插件
const CopyWebpackPlugin = require('copy-webpack-plugin'); //拷贝目录
// require('font-awesome-webpack');


{
  let data = fs.readFileSync('./src/config.js');
  data = data.toString();

  // 更改ajax url api
  if( process.env.NODE_ENV == 'production' ){ // 生产环境的环境变量

    data = data.replace(/'([^']+)'/, function($0, $1){
      return "'http://47.98.248.65:9000/api'";
    });

    fs.writeFileSync('./src/config.js', data);
  }

}

module.exports = {
  mode: 'development',
  entry: {
    login: './src/page/login/index.js',
    admin: './src/page/admin/index.js',
    admin_role:'./src/page/admin-folder/role/index.js',
    admin_employee:'./src/page/admin-folder/employee/index.js'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].bundle.js',
  },
  devServer:{
    contentBase:path.resolve('./static'),
    port:9000
  },
  module:{
    rules: [
          { test: /\.scss$/,
            use: ExtractTextPlugin.extract({
              fallback:"style-loader",
              use: ['css-loader','sass-loader'],
              publicPath: "/dist"
            })},
            {
                test: /\.js$/,
                 exclude: /node_modules/,
                 loader: "babel-loader"
            },

        ]
      },
      plugins: [
        new ExtractTextPlugin("[name].bundle.css",{allChunks: true}),
        new CopyWebpackPlugin([{
          from: path.resolve(__dirname, './src/public'),
          to: path.resolve(__dirname, './dist/common'),
          force: true
        }]),
        new CleanWebpackPlugin(['dist']),
      ],
      performance: {
        hints: false, // 文件大小提示
      }
};
